package data.datas;

import java.io.IOException;

import network.e2e.FrameReader;
import network.e2e.FrameWriter;

public class DatasParser {
  
  /**
   * Datas IDs
   */
  private static final String COMPLETE_HISTORY_DATAS = "DTS_CMPLTHISTORY";
  private static final String NO_HISTORY_DATAS = "DTS_NOHISTORY";
  
  /**
   * Decode a datas from the network
   * @requires reader not null
   * @param reader the net
   * @return the data read
   * @throws IOException 
   */
  public static Datas retrieve(FrameReader reader) throws IOException {
    assert (reader != null);
    String datas_id = reader.readString();
    assert (datas_id != null);
    if (datas_id.equals(COMPLETE_HISTORY_DATAS)) {
      return new CompleteHistoryDatas(reader);
    } else if (datas_id.equals(NO_HISTORY_DATAS)) {
      return new NoHistoryDatas(reader);
    } else {
      assert false :
        "DatasParser: Unrecognized Content ID";
      return null;
    }
  }
  
  /**
   * Encode a datas to be sent over the network
   * @requires param not null
   * @param datas the datas
   * @param writer  the net
   * @throws IOException 
   */
  public static void send(Datas datas, FrameWriter writer) throws IOException {
    assert datas != null && writer != null;
    
    if (datas instanceof CompleteHistoryDatas) {
      writer.writeString(COMPLETE_HISTORY_DATAS);
    } else if (datas instanceof NoHistoryDatas) {
      writer.writeString(NO_HISTORY_DATAS);
    } else {
      assert false :
        "DatasParser: Unrecognized Content";
    }
    datas.send(writer);
  }
  
}
